package com.zgys.mes.service;

import com.github.pagehelper.PageHelper;
import com.zgys.mes.client.dto.PageInfoDto;
import com.zgys.mes.client.dto.UserDto;
import com.zgys.mes.dao.entity.User;
import com.zgys.mes.dao.mapper.CommonMapper;
import com.zgys.mes.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by yangjf on 2017/11/23.
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CommonMapper commonMapper;


    public PageInfoDto listUser(int page, int size) {
        PageHelper.startPage(page, size, true);
        List<User> users= userMapper.selectAll();
        PageInfoDto<User> pageInfoDto = new PageInfoDto<>(users);
        List<UserDto> userDtos = new ArrayList<>();
        for (User user : users) {
            UserDto userDto = new UserDto();
            userDto.setId(user.getId());
            userDto.setName(user.getName());
            userDto.setCreatetime(user.getCreatetime());
            userDtos.add(userDto);
        }
        pageInfoDto.setData(userDtos);
        return pageInfoDto;
    }

    public UserDto getUser(int userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        UserDto userDto = new UserDto();
        userDto.setId(user.getId());
        userDto.setName(user.getName());
        userDto.setCreatetime(user.getCreatetime());
        return userDto;
    }

    public List<LinkedHashMap<String,Object>> selectall(int page, int size) {
        PageHelper.startPage(page,size);
        return commonMapper.select("select * from mes_user");
    }


}
